//
//  MovieViewController.m
//  豆瓣
//
//  Created by aaaaaa on 16/6/23.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "MovieViewController.h"
#import "MovieListModel.h"
#import "MovieTableViewCell.h"
#import "MovieIntroducedViewController.h"
#import "Tools.h"
#import "MovieCollectionViewCell.h"

@interface MovieViewController ()<UITableViewDataSource, UITableViewDelegate, UICollectionViewDataSource, UICollectionViewDelegate>

@property (nonatomic, retain) NSMutableArray<MovieListModel *> *modelArray;
@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic ,retain) UICollectionView *collectionView;


@end

@implementation MovieViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self config];
    
    [self handleData];
    [self createRightBar];
    [self createTableView];
    [self createCollectionView];
}

- (void)config{
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:86 / 255.0 green:137 / 255.0 blue:157 / 255.0 alpha:1.0];
    self.navigationItem.title = @"电影";
    self.automaticallyAdjustsScrollViewInsets = NO;
    
}

- (void)handleData{
    
    self.modelArray = [NSMutableArray array];
    
    NSString *path = [[NSBundle mainBundle] pathForResource:@"movielist" ofType:@"txt"];
    
    NSError *error = nil;
    NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:[NSData dataWithContentsOfFile:path] options:0 error:&error];
    NSArray *array = [dictionary objectForKey:@"result"];
    
    for (NSDictionary *dic in array) {
        MovieListModel *model = [[MovieListModel alloc] init];
        [model setValuesForKeysWithDictionary:dic];
        [self.modelArray addObject:model];
        [model release];
    }
    
}

- (void)createRightBar{
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[IMAGE(@"btn_nav_collection@2x", @"png") imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(rightAction:)];
}

- (void)rightAction:(UIBarButtonItem *)bar{
    
    if (self.collectionView.hidden) {
        self.tableView.hidden = YES;
        self.collectionView.hidden = NO;
        bar.image = [IMAGE(@"btn_nav_list@2x", @"png") imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        
    } else {
        self.tableView.hidden = NO;
        self.collectionView.hidden = YES;
        bar.image = [IMAGE(@"btn_nav_collection@2x", @"png") imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    }
    
    
}


//tableView
- (void)createTableView{
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, self.view.bounds.size.width, self.view.bounds.size.height - 64 - 50)];
    
    self.tableView.separatorColor = [UIColor whiteColor];

    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    [self.view addSubview:self.tableView];
    [self.tableView release];
    
    [self.tableView registerClass:[MovieTableViewCell class] forCellReuseIdentifier:@"movieCell"];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.modelArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    MovieTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"movieCell"];
    MovieListModel *model = self.modelArray[indexPath.row];
    cell.movieListModel = model;
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 180;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [self jumpToIntrodeucedVc:indexPath];
}






//collectionView
- (void)createCollectionView{
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    
    flowLayout.itemSize = CGSizeMake(110, 200);
    
    flowLayout.minimumLineSpacing = 10;
    
    flowLayout.sectionInset = UIEdgeInsetsMake(20, 20, 20, 20);
    
    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 64, self.view.bounds.size.width, self.view.bounds.size.height - 64 - 50) collectionViewLayout:flowLayout];
    [flowLayout release];
    
    self.collectionView.backgroundColor = [UIColor whiteColor];
    
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    
    [self.collectionView registerClass:[MovieCollectionViewCell class] forCellWithReuseIdentifier:@"movieCollectCell"];
    
    self.collectionView.hidden = YES;
    [self.view addSubview:self.collectionView];
    [self.collectionView release];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.modelArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    MovieCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"movieCollectCell" forIndexPath:indexPath];
    
    cell.movieListModel = self.modelArray[indexPath.row];
    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    [self jumpToIntrodeucedVc:indexPath];
}


//跳转到第二个界面{
- (void)jumpToIntrodeucedVc:(NSIndexPath *)indexPath{
    MovieIntroducedViewController *movieIntroducedVc = [[MovieIntroducedViewController alloc] init];
    movieIntroducedVc.movieListModel = self.modelArray[indexPath.row];
    [self.navigationController pushViewController:movieIntroducedVc animated:YES];
    [movieIntroducedVc release];
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
